package com.shop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.db.DBConfiguration;
import org.apache.hadoop.mapreduce.lib.db.DBOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.net.URI;

public class Step9 extends Configured implements Tool {
    static class Step9Mapper extends Mapper<Text, DoubleWritable, Text, DoubleWritable> {
        @Override
        protected void map(Text key, DoubleWritable value, Mapper<Text, DoubleWritable, Text, DoubleWritable>.Context context) throws IOException, InterruptedException {
            context.write(key, value);
        }
    }

    static class Step9Reducer extends Reducer<Text, DoubleWritable, Shop, NullWritable> {
        @Override
        protected void reduce(Text key, Iterable<DoubleWritable> values, Reducer<Text, DoubleWritable, Shop, NullWritable>.Context context) throws IOException, InterruptedException {
            String[] datas = key.toString().split(",");
            Double val = 0.0;
            for (DoubleWritable value : values) {
                val = value.get();
            }
            Shop shop = new Shop(datas[0], datas[1], val);
            context.write(shop, null);
        }
    }

    @Override
    public int run(String[] strings) throws Exception {
        Configuration conf = getConf();
        // 指定输入输出路径
        Path step9input1 = new Path(
                "hdfs://192.168.10.11:9000/shop8output");
        DBConfiguration.configureDB(conf,
                "com.mysql.cj.jdbc.Driver",
                "jdbc:mysql://192.168.10.11:3306/shops",
                "root",
                "root");

        //构建Job
        Job job = Job.getInstance(conf);
        job.setJobName("step9");
        job.setJarByClass(this.getClass());

        job.setMapperClass(Step9Mapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(DoubleWritable.class);

        job.setReducerClass(Step9Reducer.class);
        job.setOutputKeyClass(Shop.class);
        job.setOutputValueClass(NullWritable.class);

        job.setInputFormatClass(SequenceFileInputFormat.class);
        SequenceFileInputFormat.addInputPath(job, step9input1);

//        job.setOutputFormatClass(TextOutputFormat.class);
//        TextOutputFormat.setOutputPath(job, step6output);
        job.setOutputFormatClass(DBOutputFormat.class);
        DBOutputFormat.setOutput(job, "t_recommend_shop",
                "user_id",
                "shops_id",
                "recommand_value");
        return job.waitForCompletion(true) ? 0 : -1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(ToolRunner.run(new Step9(), args));
    }
}
